package com.example.homework7;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.sql.SQLData;

public class jicao extends Fragment {
    private Button button6;
    private Button button8;
    private EditText editTextTextPersonName4,editTextTextPersonName5,getEditTextTextPersonName6;
    private TextView tv;

    public jicao (){
        super(R.layout.jicao);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button button6=(Button) getView().findViewById(R.id.button6);
        Button button8=(Button) getView().findViewById(R.id.button8);
        EditText editTextTextPersonName4=(EditText) getView().findViewById(R.id.editTextTextPersonName4);
        EditText editTextTextPersonName5=(EditText) getView().findViewById(R.id.editTextTextPersonName5);
        EditText editTextTextPersonName6=(EditText) getView().findViewById(R.id.editTextTextPersonName6);
        TextView tv=(TextView) getView().findViewById(R.id.tv);

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbHelper dbHelper=new DbHelper(getContext());
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("id",editTextTextPersonName6.getText().toString());
                values.put("username",editTextTextPersonName4.getText().toString());
                values.put("age",editTextTextPersonName5.getText().toString());

                long ret=db.insert("users",null,values);
                String msg=ret==-1? "添加失败":"添加成功";
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                db.close();
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbHelper dbHelper=new DbHelper(getContext());
                SQLiteDatabase db=dbHelper.getReadableDatabase();
                StringBuilder stringBuilder=new StringBuilder();

                Cursor cursor=db.query("users",null,"age<18",null,null,null,null);
                while(cursor.moveToNext()){
                    String username =cursor.getString(cursor.getColumnIndex("username"));
                    long age =cursor.getLong(cursor.getColumnIndex("age"));
                    int id=cursor.getInt(cursor.getColumnIndex("id"));
                    stringBuilder.append("id:").append(id)
                            .append("  username:").append(username)
                            .append("  age:").append(age)
                            .append("\n");

                }
                tv.setText(stringBuilder.toString());
            }

        });
    }
}
